/*
 * @Author: your name
 * @Date: 2021-11-10 08:34:30
 * @LastEditTime: 2021-11-18 10:12:06
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \li-yuwei\egg-example\app\service\login.js
 */

const Service = require("egg").Service;
const { md5 } = require("utility");
const jwt = require("jsonwebtoken");
const timeFormart = require("../utils/timeFormart");
class UserService extends Service {
    //登录
    async login() {
            const { ctx, app } = this;
            const { username, password } = ctx.request.body;
            const isLogin = await app.mysql.get("users", {
                username,
                password: md5(String(password)),
            });
            if (isLogin) {
                //判断是否禁用
                if (isLogin.state * 1) {
                    //用户信息
                    const userData = {
                        id: isLogin.id,
                        nickname: isLogin.nickname,
                        identity: isLogin.identity,
                        username: isLogin.username,
                    };
                    return {
                        userData,
                        token: jwt.sign({
                                id: isLogin.id,
                                nickname: isLogin.nickname,
                            },
                            app.config.keys
                        ),
                    };
                } else {
                    return "用户已禁用";
                }
            } else {
                return "账号密码有误";
            }
        }
        //注册
    async register() {
            const { ctx, app } = this;
            const { username, password, nickname } = ctx.request.body;
            // 1. 判断账号存不存在
            const isHave = await app.mysql.get("users", { username });
            if (isHave) {
                return "账号已经存在";
            } else {
                const res = await app.mysql.insert("users", {
                    username,
                    password: md5(String(password)),
                    nickname,
                    identity: "访客",
                    reg_time: timeFormart(Date.now()),
                    state: 1,
                });
                return res;
            }
        }
        //修改密码
    async changePassword() {
            const { ctx, app } = this;
            const { username, oldPwd, newPwd } = ctx.request.body;
            const isTrue = await app.mysql.get("users", { username });
            //判断输入旧密码是否相同
            if (isTrue.password === md5(String(oldPwd))) {
                if (oldPwd === newPwd) return "新旧密码不得相同"; //修改密码不得相同
                //更新密码
                const res = await app.mysql.update("users", {
                    id: isTrue.id,
                    password: md5(String(newPwd)),
                });
                return {
                    res,
                };
            } else {
                return "原密码错误";
            }
        }
        //获取用户信息(单个)
    async getUserInfo() {
            const { ctx, app } = this;
            const { id } = ctx.request.body;
            //获取信息
            const userInfo = await app.mysql.get("users", { id });
            //返回内容中删除密码字段
            delete userInfo.password;
            //返回信息
            return userInfo;
        }
        //获取用户信息(全部)
    async getAllUser() {
            const { ctx, app } = this;
            const { pageSize, pageIndex, username, email, identity, state } =
            ctx.request.body;
            let res = await app.mysql.select("users");
            let total = res.length;
            //用户名搜索
            if (username) {
                res = res.filter((item) => {
                    if (
                        item.username.indexOf(username) !== -1 ||
                        item.username.includes(username)
                    ) {
                        return item;
                    }
                });
                total = res.length;
            }
            //邮箱搜索
            if (email) {
                res = res.filter((item) => {
                    if (item.email !== null) {
                        if (item.email.indexOf(email) !== -1 || item.email.includes(email)) {
                            return item;
                        }
                    }
                });
                total = res.length;
            }
            //启用状态搜索
            if (state !== null) {
                res = res.filter((item) => item.state * 1 === state * 1);
                total = res.length;
            }
            //身份搜索
            if (identity) {
                res = res.filter((item) => item.identity === identity);
                total = res.length;
            }
            //分页
            res = res.slice((pageIndex - 1) * pageSize, pageSize * pageIndex);
            //时间倒序
            res.sort((a, b) => {
                return new Date(b.reg_time) - new Date(a.reg_time);
            });
            return { res, total };
        }
        //修改用户信息
    async editUserInfo() {
            const { ctx, app } = this;
            const { id, username, email, avatar } = ctx.request.body;
            const isHaveUser = await app.mysql.get("users", { username });
            //判断用户名是否存在(不包括自己)
            if (isHaveUser && isHaveUser.id !== id) return "用户名已存在";
            //获取信息
            const userInfo = await app.mysql.update("users", {
                id,
                username,
                email,
                avatar,
            });
            return userInfo;
        }
        //改变用户可用状态
    async changeUserState() {
            const { ctx, app } = this;
            const { id } = ctx.request.body;
            //找到原数据
            const res = await app.mysql.get("users", {
                id,
            });
            //判断状态
            if (res.state * 1 === 1) {
                //更新用户可用状态
                await app.mysql.update("users", { id, state: 0 });
            } else {
                //更新用户可用状态
                await app.mysql.update("users", { id, state: 1 });
            }
            return "操作成功";
        }
        //改变用户身份
    async changeUseIdentity() {
            const { ctx, app } = this;
            const { id } = ctx.request.body;
            //找到原数据
            const res = await app.mysql.get("users", {
                id,
            });
            //判断状态
            if (res.identity === "访客") {
                //更新用户可用状态
                await app.mysql.update("users", { id, identity: "管理员" });
            } else {
                //更新用户可用状态
                await app.mysql.update("users", { id, identity: "访客" });
            }
            return "操作成功";
        }
        //批量启用
    async batchUserStart() {
            const { ctx, app } = this;
            const { batchList } = ctx.request.body;
            batchList.forEach((item) => {
                app.mysql.update("users", {
                    id: item.id,
                    state: 1,
                });
            });
            return "操作成功";
        }
        //批量禁用
    async batchUserForbid() {
            const { ctx, app } = this;
            const { batchList } = ctx.request.body;
            batchList.forEach((item) => {
                app.mysql.update("users", {
                    id: item.id,
                    state: 0,
                });
            });
            return "操作成功";
        }
        //批量授权
    async batchUserAllow() {
            const { ctx, app } = this;
            const { batchList } = ctx.request.body;
            batchList.forEach((item) => {
                app.mysql.update("users", {
                    id: item.id,
                    identity: "管理员",
                });
            });
            return "操作成功";
        }
        //批量解除授权
    async batchUserRelieve() {
        const { ctx, app } = this;
        const { batchList } = ctx.request.body;
        batchList.forEach((item) => {
            app.mysql.update("users", {
                id: item.id,
                identity: "访客",
            });
        });
        return "操作成功";
    }
}
module.exports = UserService;